package data.InformData;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import po.informPO.InformPO;
import po.informPO.StudentInformPO;
import po.informPO.TeacherInformPO;
import dataservice.ResultMessage;
import dataservice.informDataService.InformDataService;

public class InformData extends UnicastRemoteObject implements
		InformDataService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public InformData() throws RemoteException {
		super();
	}

	@Override
	public ResultMessage insert(InformPO inform) throws RemoteException {
		int result = -1;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/选课管理系统", "root", "");
			Statement statement = conn.createStatement();
			String head = "INSERT INTO `选课管理系统`.`inform`";
			String colomnString = "(`id`, `message`)";
			String dataString = "VALUES (" + getValue(inform.getId());
			dataString = dataString + "," + getValue(inform.getMessage()) + ")";
			String sqlString = head + colomnString + dataString;
			result = statement.executeUpdate(sqlString);
		} catch (SQLException e) {
			System.out.printf("ErrorCode is %d\n", e.getErrorCode());
			System.out.printf("ErrorMessage is %s\n", e.getMessage());
			if (e.getErrorCode() == 1062) {
				return ResultMessage.INSERT_DEPLICATE_PRIMARY;
			}
			if (e.getErrorCode() == 1364) {
				return ResultMessage.INSERT_ISNULL;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result == 1) {
			return ResultMessage.INSERT_SUCCESS;
		} else {
			return ResultMessage.INSERT_FAILURE;
		}
	}

	@Override
	public ResultMessage deleteAll() throws RemoteException {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/选课管理系统", "root", "");
			Statement statement = conn.createStatement();
			String sqlString = "TRUNCATE TABLE inform";
			statement.execute(sqlString);
		} catch (SQLException e) {
			System.out.printf("ErrorCode is %d\n", e.getErrorCode());
			System.out.printf("ErrorMessage is %s\n", e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultMessage.DELETE_SUCCESS;
	}

	@Override
	public ArrayList<StudentInformPO> findAllByStudentID(String studentID) throws RemoteException {
		ArrayList<StudentInformPO> informs= new ArrayList<StudentInformPO>();
		StudentInformPO studentInform = null;
		String id = null;
		String message = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/选课管理系统", "root", "");
			Statement statement = conn.createStatement();
			String sqlString = "SELECT inform.* FROM inform "
					+ "WHERE inform.id='"+studentID+"'";
			System.out.println(sqlString);
			ResultSet rs = statement.executeQuery(sqlString);
			while (rs.next()) {
				id = rs.getString(1);
				message = rs.getString(2);
				studentInform = new StudentInformPO(id, message);
				informs.add(studentInform);
			}
		} catch (SQLException e) {
			System.out.printf("ErrorCode is %d\n", e.getErrorCode());
			System.out.printf("ErrorMessage is %s\n", e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return informs;
	}

	private String getValue(String valueString) {
		String result = null;
		if (!((valueString == null) || (valueString.isEmpty()))) {
			result = "'" + valueString + "'";
		} else {
			result = "NULL";
		}
		return result;
	}

	@Override
	public ArrayList<TeacherInformPO> findAllByTeacherID(String teacherId)
			throws RemoteException {
		ArrayList<TeacherInformPO> informs= new ArrayList<TeacherInformPO>();
		TeacherInformPO teacherInform = null;
		String id = null;
		String message = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/选课管理系统", "root", "");
			Statement statement = conn.createStatement();
			String sqlString = "SELECT inform.* FROM inform "
					+ "WHERE inform.id='"+teacherId+"'";
			System.out.println(sqlString);
			ResultSet rs = statement.executeQuery(sqlString);
			while (rs.next()) {
				id = rs.getString(1);
				message = rs.getString(2);
				teacherInform = new TeacherInformPO(id, message);
				informs.add(teacherInform);
			}
		} catch (SQLException e) {
			System.out.printf("ErrorCode is %d\n", e.getErrorCode());
			System.out.printf("ErrorMessage is %s\n", e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return informs;
	}
}
